<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>聊天输入框</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      font-family: "Microsoft YaHei", sans-serif;
      background-color: #f5f6fa;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
    }

    .chat-container {
      width: 100%;
      max-width: 500px;
      background-color: #ffffff;
      border-radius: 12px;
      box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
      overflow: hidden;
    }

    .chat-header {
      background-color: #007bff;
      color: white;
      padding: 15px;
      text-align: center;
      font-size: 18px;
      font-weight: bold;
    }

    .chat-messages {
      height: 300px;
      padding: 15px;
      overflow-y: auto;
      background-color: #f9f9fb;
      border-bottom: 1px solid #e0e0e0;
    }

    .message {
      margin-bottom: 10px;
      padding: 8px 12px;
      background-color: #e9ecef;
      border-radius: 18px;
      display: inline-block;
      max-width: 80%;
      word-wrap: break-word;
    }

    .input-area {
      padding: 15px;
      border-top: 1px solid #e0e0e0;
      display: flex;
      flex-direction: column;
      gap: 10px;
    }

    .input-toolbar {
      display: flex;
      gap: 15px;
      padding: 0 5px;
    }

    .tool-btn {
      background: none;
      border: none;
      font-size: 20px;
      cursor: pointer;
      color: #6c757d;
      transition: all 0.2s ease;
      width: 36px;
      height: 36px;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .tool-btn:hover {
      background-color: #f0f0f0;
      transform: scale(1.1);
      color: #007bff;
    }

    .input-box {
      position: relative;
      border: 1px solid #ddd;
      border-radius: 24px;
      padding: 10px 15px;
      display: flex;
      align-items: center;
      background-color: #ffffff;
      box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
    }

    #message-input {
      flex: 1;
      border: none;
      outline: none;
      font-size: 16px;
      resize: none;
      max-height: 120px;
      min-height: 24px;
      padding: 5px 0;
      line-height: 1.5;
    }

    #message-input::placeholder {
      color: #aaa;
    }

    .send-btn {
      background-color: #007bff;
      color: white;
      border: none;
      width: 40px;
      height: 40px;
      border-radius: 50%;
      cursor: pointer;
      font-size: 18px;
      display: flex;
      align-items: center;
      justify-content: center;
      transition: all 0.3s ease;
      margin-left: 10px;
    }

    .send-btn:hover {
      background-color: #0056b3;
      transform: scale(1.05);
    }

    .hidden {
      display: none;
    }

    .emoji-picker {
      position: absolute;
      bottom: 100%;
      left: 20px;
      background-color: white;
      border-radius: 12px;
      box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
      padding: 10px;
      display: grid;
      grid-template-columns: repeat(7, 1fr);
      gap: 8px;
      z-index: 10;
      animation: fadeIn 0.2s ease-out;
    }

    .emoji-picker button {
      font-size: 18px;
      border: none;
      background: none;
      cursor: pointer;
      padding: 5px;
      border-radius: 8px;
      transition: background-color 0.2s;
    }

    .emoji-picker button:hover {
      background-color: #f0f0f0;
    }

    @keyframes fadeIn {
      from {
        opacity: 0;
        transform: translateY(10px);
      }
      to {
        opacity: 1;
        transform: translateY(0);
      }
    }

    .file-preview {
      display: flex;
      gap: 10px;
      flex-wrap: wrap;
      margin-top: 10px;
    }

    .preview-item {
      position: relative;
      width: 80px;
      height: 80px;
      border-radius: 10px;
      overflow: hidden;
      box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
    }

    .preview-item img, .preview-item .file-icon {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }

    .preview-item .file-icon {
      background-color: #007bff;
      color: white;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 24px;
    }

    .remove-preview {
      position: absolute;
      top: -8px;
      right: -8px;
      width: 20px;
      height: 20px;
      background-color: #ff4757;
      color: white;
      border: none;
      border-radius: 50%;
      font-size: 12px;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  </style>
</head>
<body>
<div class="chat-container">
  <div class="chat-header">聊天应用</div>
  <div class="chat-messages" id="chat-messages">
    <div class="message">你好！这是一个支持表情、图片和文件的聊天输入框。</div>
  </div>

  <div class="input-area">
    <!-- 工具栏：表情、图片、文件 -->
    <div class="input-toolbar">
      <button class="tool-btn" id="emoji-btn">😊</button>
      <button class="tool-btn" id="image-btn">🖼️</button>
      <button class="tool-btn" id="file-btn">📎</button>
    </div>

    <!-- 文件预览区域 -->
    <div class="file-preview hidden" id="file-preview"></div>

    <!-- 主输入框 -->
    <div class="input-box">
        <textarea
                id="message-input"
                placeholder="输入消息..."
                rows="1"></textarea>
      <button class="send-btn" id="send-btn">➤</button>
    </div>
  </div>
</div>

<script>
  // 获取DOM元素
  const messageInput = document.getElementById('message-input');
  const chatMessages = document.getElementById('chat-messages');
  const sendButton = document.getElementById('send-btn');
  const emojiButton = document.getElementById('emoji-btn');
  const imageButton = document.getElementById('image-btn');
  const fileButton = document.getElementById('file-btn');
  const filePreview = document.getElementById('file-preview');

  // 表情包列表
  const emojis = ['😀', '😁', '😂', '🤣', '😊', '😍', '🤔', '😎', '😭', '😱', '👍', '👏', '🎉', '❤️', '🙏'];

  // 用于存储临时选择的文件
  let selectedFiles = [];

  // 动态调整输入框高度
  messageInput.addEventListener('input', function () {
    this.style.height = 'auto';
    this.style.height = (this.scrollHeight) + 'px';
  });

  // 发送消息函数
  function sendMessage() {
    const message = messageInput.value.trim();

    if (message || selectedFiles.length > 0) {
      const messageDiv = document.createElement('div');
      messageDiv.classList.add('message');

      // 添加文本消息
      if (message) {
        messageDiv.textContent = message;
      }

      // 添加文件预览到消息
      selectedFiles.forEach(file => {
        const filePreviewInMsg = document.createElement('div');
        filePreviewInMsg.style.marginTop = '5px';
        filePreviewInMsg.style.fontSize = '12px';
        filePreviewInMsg.style.color = '#007bff';
        filePreviewInMsg.textContent = `📎 ${file.name}`;
        messageDiv.appendChild(filePreviewInMsg);
      });

      chatMessages.appendChild(messageDiv);
      chatMessages.scrollTop = chatMessages.scrollHeight;

      // 清空输入框和文件
      messageInput.value = '';
      messageInput.style.height = 'auto';
      clearFilePreview();
    }
  }

  // 点击发送按钮
  sendButton.addEventListener('click', sendMessage);

  // 回车发送，Shift+回车换行
  messageInput.addEventListener('keydown', function(e) {
    if (e.key === 'Enter' && !e.shiftKey) {
      e.preventDefault();
      sendMessage();
    }
  });

  // 创建表情选择器
  function createEmojiPicker() {
    const picker = document.createElement('div');
    picker.classList.add('emoji-picker');

    emojis.forEach(emoji => {
      const btn = document.createElement('button');
      btn.textContent = emoji;
      btn.addEventListener('click', () => {
        messageInput.value += emoji;
        messageInput.focus();
        picker.remove();
      });
      picker.appendChild(btn);
    });

    return picker;
  }

  // 显示/隐藏表情选择器
  let emojiPicker = null;
  emojiButton.addEventListener('click', () => {
    if (emojiPicker && !document.body.contains(emojiPicker)) {
      emojiPicker = null;
    }

    if (emojiPicker) {
      emojiPicker.remove();
      emojiPicker = null;
    } else {
      emojiPicker = createEmojiPicker();
      const inputBox = document.querySelector('.input-box');
      inputBox.appendChild(emojiPicker);
      messageInput.focus();
    }
  });

  // 图片上传处理
  imageButton.addEventListener('click', () => {
    const input = document.createElement('input');
    input.type = 'file';
    input.accept = 'image/*';
    input.multiple = true;
    input.style.display = 'none';
    input.onchange = (e) => {
      const files = Array.from(e.target.files);
      handleFiles(files);
    };
    document.body.appendChild(input);
    input.click();
    document.body.removeChild(input);
  });

  // 文件上传处理
  fileButton.addEventListener('click', () => {
    const input = document.createElement('input');
    input.type = 'file';
    input.multiple = true;
    input.style.display = 'none';
    input.onchange = (e) => {
      const files = Array.from(e.target.files).filter(f => !f.type.startsWith('image/'));
      handleFiles(files);
    };
    document.body.appendChild(input);
    input.click();
    document.body.removeChild(input);
  });

  // 处理文件选择
  function handleFiles(files) {
    if (files.length === 0) return;

    selectedFiles = selectedFiles.concat(files);
    updateFilePreview();
  }

  // 更新文件预览
  function updateFilePreview() {
    filePreview.classList.remove('hidden');
    filePreview.innerHTML = '';

    selectedFiles.forEach((file, index) => {
      const item = document.createElement('div');
      item.classList.add('preview-item');

      if (file.type.startsWith('image/')) {
        const img = document.createElement('img');
        img.src = URL.createObjectURL(file);
        item.appendChild(img);
      } else {
        const icon = document.createElement('div');
        icon.classList.add('file-icon');
        icon.textContent = '📄';
        item.appendChild(icon);
      }

      const removeBtn = document.createElement('button');
      removeBtn.classList.add('remove-preview');
      removeBtn.textContent = '×';
      removeBtn.onclick = () => {
        selectedFiles.splice(index, 1);
        updateFilePreview();
        if (selectedFiles.length === 0) {
          filePreview.classList.add('hidden');
        }
      };
      item.appendChild(removeBtn);

      filePreview.appendChild(item);
    });
  }

  // 清空预览
  function clearFilePreview() {
    selectedFiles = [];
    filePreview.classList.add('hidden');
    filePreview.innerHTML = '';
  }

  // 点击外部关闭表情选择器
  document.addEventListener('click', (e) => {
    if (emojiPicker && !emojiButton.contains(e.target) && !emojiPicker.contains(e.target)) {
      emojiPicker.remove();
      emojiPicker = null;
    }
  });
</script>
</body>
</html>